﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace JewererBasic.Classes
{
    public class WasteReportHelper
    {
        private DateTime? dateTime;

        private List<GetWorkerIdInMonth_Result> listWasteDetail;
        private DateTime? time;

        public WasteReportHelper(DateTime? time)
        {
            // TODO: Complete member initialization
            this.time = time;
        }

        //public WasteReportHelper(DateTime? date)
        //{
        //    this.dateTime = date;
        //    MSQDBEntities dc = new MSQDBEntities();
        //    this.listWasteDetail = dc.GetWorkerIdInMonth(date).OrderBy(m => m.WorkerId).ToList();
        //    if (listWasteDetail.Count > 0)
        //    {
        //        /* Generate data for Waste Report datasource */
        //        this.WorkerId_Waste = new List<GetWorkerId_Waste_Result>();
        //        string currentWorker = listWasteDetail[0].WorkerId;
        //        double totalWeight = 0;
        //        foreach (var item in listWasteDetail)
        //        {
        //            if (item.WorkerId.Equals(currentWorker))
        //            {
        //                // Calculate total waste of a worker id
        //                totalWeight += item.WeightOut.Value - item.WeigthIn.Value;
        //            }
        //            else
        //            {
        //                //if (totalWeight != 0)
        //                //{
        //                    this.WorkerId_Waste.Add(new GetWorkerId_Waste_Result()
        //                    {
        //                        WorkerID = currentWorker,
        //                        TotalWaste = (float)totalWeight
        //                    });
        //                //}  
        //                currentWorker = item.WorkerId;
        //                totalWeight = item.WeightOut.Value - item.WeigthIn.Value;
        //            }
        //        }
        //        this.WorkerId_Waste.Add(new GetWorkerId_Waste_Result()
        //        {
        //            WorkerID = currentWorker,
        //            TotalWaste = (float)totalWeight
        //        });
        //        /* end generate data for main report */
        //    }
        //}

        public List<GetWorkerId_Waste_Result> WorkerId_Waste { get; set; }

        /* Generate Datasource for subreport */
        public List<GetWaste_Detail_Result> GetWasteDetail(string workerId)
        {
            List<GetWaste_Detail_Result> result = new List<GetWaste_Detail_Result>();
            foreach (var item in this.listWasteDetail)
            {
                if (item.WorkerId.Equals(workerId))
                {
                    GetWaste_Detail_Result d = new GetWaste_Detail_Result();
                    d.BagId = item.IdBag;
                    d.DateIn = item.DateIn;
                    d.DateOut = item.DateOut;
                    d.MC = item.MC;
                    d.ProcessName = item.ProcessName;
                    d.WeightIn = item.WeigthIn;
                    d.WeightOut = item.WeightOut;
                    d.Waste = d.WeightOut - d.WeightIn;
                    result.Add(d);
                }
            }
            return result;
        }
    }
}